package cn.flysheep;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FlySheep on 17/1/16.
 */
public class Pagination extends SimplePage {

    private List<?> list;

    private List<String> pageView;

    public Pagination() {
    }

    public Pagination(int pageNO, int pageSize, int totalCount) {
        super(pageNO, pageSize, totalCount);
    }

    public Pagination(int pageNO, int pageSize, int totalCount, List<?> list) {
        super(pageNO, pageSize, totalCount);
        this.list = list;
    }

    public int getFirstResult() {
        return (this.pageNO - 1) * this.pageSize;
    }

    public List<?> getList() {
        return this.list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public List<String> getPageView() {
        return pageView;
    }

    public void setPageView(List<String> pageView) {
        this.pageView = pageView;
    }

    public void pageView(String url, String params) {
        this.pageView = new ArrayList();
        if (this.pageNO != 1) {
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=1'\"><font size=2>首页</font></a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (this.pageNO - 1) + "'\"><font size=2>上一页</font></a>");
        } else {
            this.pageView.add("<font size=2>首页</font>");
            this.pageView.add("<font size=2>上一页</font>");
        }
        if (getTotalPage() <= 10) {
            for (int i = 0; i < getTotalPage(); i++) {
                if (i + 1 == this.pageNO) {
                    this.pageView.add("<strong>" + this.pageNO + "</strong>");
                    i++;
                    if (this.pageNO == getTotalPage()) {
                        break;
                    }
                }
                this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (i + 1) + "'\">" + (i + 1) + "</a>");
            }
        } else if (getTotalPage() <= 20) {
            int l = 0;
            int r = 0;
            if (this.pageNO < 5) {
                l = this.pageNO - 1;
                r = 10 - l - 1;
            } else if (getTotalPage() - this.pageNO < 5) {
                r = getTotalPage() - this.pageNO;
                l = 9 - r;
            } else {
                l = 4;
                r = 5;
            }
            int tmp = this.pageNO - l;
            for (int i = tmp; i < tmp + 10; i++) {
                if (i == this.pageNO) {
                    this.pageView.add("<strong>" + this.pageNO + "</strong>");
                    i++;
                    if (this.pageNO == getTotalPage()) {
                        break;
                    }
                }
                this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + i + "'\">" + i + "</a>");
            }
        } else if (this.pageNO < 7) {
            for (int i = 0; i < 8; i++) {
                if (i + 1 == this.pageNO) {
                    this.pageView.add("<strong>" + this.pageNO + "</strong>");
                    i++;
                }
                this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (i + 1) + "'\">" + (i + 1) + "</a>");
            }
            this.pageView.add("...");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (getTotalPage() - 1) + "'\">" + (getTotalPage() - 1) + "</a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + getTotalPage() + "'\">" + getTotalPage() + "</a>");
        } else if (this.pageNO > getTotalPage() - 6) {
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + 1 + "'\">" + 1 + "</a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + 2 + "'\">" + 2 + "</a>");
            this.pageView.add("...");
            for (int i = getTotalPage() - 8; i < getTotalPage(); i++) {
                if (i + 1 == this.pageNO) {
                    this.pageView.add("<strong>" + this.pageNO + "</strong>");
                    i++;
                    if (this.pageNO == getTotalPage()) {
                        break;
                    }
                }
                this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (i + 1) + "'\">" + (i + 1) + "</a>");
            }
        } else {
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + 1 + "'\">" + 1 + "</a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + 2 + "'\">" + 2 + "</a>");
            this.pageView.add("...");

            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (this.pageNO - 2) + "'\">" + (this.pageNO - 2) + "</a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (this.pageNO - 1) + "'\">" + (this.pageNO - 1) + "</a>");
            this.pageView.add("<strong>" + this.pageNO + "</strong>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (this.pageNO + 1) + "'\">" + (this.pageNO + 1) + "</a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (this.pageNO + 2) + "'\">" + (this.pageNO + 2) + "</a>");

            this.pageView.add("...");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (getTotalPage() - 1) + "'\">" + (getTotalPage() - 1) + "</a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + getTotalPage() + "'\">" + getTotalPage() + "</a>");
        }
        if (this.pageNO != getTotalPage()) {
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + (this.pageNO + 1) + "'\"><font size=2>下一页</font></a>");
            this.pageView.add("<a href=\"javascript:void(0);\" onclick=\"javascript:window.location.href='" + url + "?" + params + "&pageNo=" + getTotalPage() + "'\"><font size=2>尾页</font></a>");
        } else {
            this.pageView.add("<font size=2>下一页</font>");
            this.pageView.add("<font size=2>尾页</font>");
        }
        this.pageView.add("共<var>" + getTotalPage() + "</var>页 到第<input type='text' id='PAGENO'  size='3' />页 <input type='button' id='skip' class='hand btn60x20' value='确定' onclick=\"javascript:window.location.href = '" + url + "?" + params + "&pageNo=' + $('#PAGENO').val() \"/>");
    }
}
